#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include <time.h>

int sockfd ;

void prinf(int signo)
{
	close(sockfd);
	printf("server is closing!\n");
	exit(0);
}

int main()
{
	sockfd = socket(PF_INET , SOCK_DGRAM , 0);
	struct sockaddr_in addr;
	addr.sin_family = PF_INET;
	addr.sin_port = htons(1333);
	addr.sin_addr.s_addr = INADDR_ANY;
	
	bind(sockfd , (struct sockaddr *)&addr , sizeof(addr));
	
	struct sockaddr_in from ;
	socklen_t len = sizeof(from);	

	while(1)
	{
		char buf[100] = {};
		recvfrom(sockfd , buf , sizeof(buf) , 0 , (struct sockaddr * )&from , &len);
		printf("buf = %s\n",buf);
		time_t curtime = time(0);
		struct tm* cur = localtime(&curtime);
		sprintf(buf , "%4d-%02d-%02d %02d:%02d:%02d",cur->tm_year+1900 , cur->tm_mon + 1 , cur->tm_mday ,cur->tm_hour, cur->tm_min, cur->tm_sec);
		sendto(sockfd , buf  , strlen(buf) , 0 , (struct sockaddr *)&from , len  );
	}
	
	close(sockfd);

}
